package com.ymatou.iapi.userservice.testcase;

import java.io.IOException;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.UserExtendingRequestDtoBean;
import com.ymatou.iapi.userservice.service.UserExtendingRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
/**
*获取用户扩展信息(目前只包含了用户是否第一次修改登录昵称)接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UserExtendingRequestDto{
	private static UserExtendingRequestDtoBean userextendingrequestdtoBean;
	private static UserExtendingRequestDtoCall userextendingrequestdtoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户扩展信息(目前只包含了用户是否第一次修改登录昵称)接口");
	}

	@Before
	public void caseUp() {
		userextendingrequestdtoBean=new UserExtendingRequestDtoBean();
		userextendingrequestdtoCall=new UserExtendingRequestDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(userextendingrequestdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserExtendingRequestDto_001() {
		Logger.start(true,"用户第一次修改登陆昵称，返回false");
		try {
			Logger.comment("注册用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId = urrc.getUserid();
			Logger.comment("验证用户是否修改过昵称");
			userextendingrequestdtoBean.setUserId(userId);
			userextendingrequestdtoBean.setExcludeNickNameModifyCheck(false);
			userextendingrequestdtoCall.setData(userextendingrequestdtoBean);
			userextendingrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.SUCCESS, userextendingrequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", userextendingrequestdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals(false, userextendingrequestdtoCall.getResult().get("IsFirstModifyNickName"), "验证是否修改过昵称了");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_UserExtendingRequestDto_002() {
		Logger.start(true,"用户不是第一次修改登陆昵称，返回true");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId = urrc.getUserid();
			Logger.comment("修改昵称");
			setNickName(userId, "nickName_"+TestDataManager.getRandomNum(8));
			Logger.comment("验证用户是否修改过昵称");
			userextendingrequestdtoBean.setUserId(userId);
			userextendingrequestdtoBean.setExcludeNickNameModifyCheck(false);
			userextendingrequestdtoCall.setData(userextendingrequestdtoBean);
			userextendingrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.SUCCESS, userextendingrequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", userextendingrequestdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyEquals(true, userextendingrequestdtoCall.getResult().get("IsFirstModifyNickName"), "验证是否修改过昵称了");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	@Ignore
	//应该没有setExcludeNickNameModifyCheck 为true的情况
	public void Tc_UserExtendingRequestDto_003() {
		Logger.start(false,"传入不需要返回是否第一次修改登陆昵称信息，则返回的结果不包含此信息");
		try {
			Logger.comment("注册用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token=registerResult.get("Token").getAsString();
			int userId = urrc.getUserid();
			Logger.comment("验证用户是否修改过昵称");
			userextendingrequestdtoBean.setUserId(userId);
			userextendingrequestdtoBean.setExcludeNickNameModifyCheck(true);
			userextendingrequestdtoCall.setData(userextendingrequestdtoBean);
			userextendingrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.SUCCESS, userextendingrequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("true", userextendingrequestdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyIsNull(userextendingrequestdtoCall.getResult().get("IsFirstModifyNickName"), "验证是否修改过昵称了");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_UserExtendingRequestDto_004() {
		Logger.start(false,"不输入用户Id,失败");
		try {
			userextendingrequestdtoBean.setExcludeNickNameModifyCheck(true);
			userextendingrequestdtoCall.setData(userextendingrequestdtoBean);
			userextendingrequestdtoCall.callService();			
			Logger.verifyEquals(ECode.ERROR400, userextendingrequestdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("false", userextendingrequestdtoCall.getString("Success"), "验证操作是否成功");
			Logger.verifyIsContains("参数非法", userextendingrequestdtoCall.getString("LastErrorMessage"), "验证Msg信息");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void setNickName(int userId, String newNickName) throws ClientProtocolException, IOException, InterruptedException{
		JUserCallService.SetNickName(userId, newNickName);
	}
}